package cn.chencaiju.algorithm;

import org.junit.Test;

public class ToeplitzMatrix {
    public boolean isToeplitzMatrix(int[][] matrix) {
    	int row=matrix.length;
    	int col=matrix[0].length;
        if(row!=col) {
        	if(row==1||col==1) {
//        		System.out.print("第一个返回");
        		return true;
        	}
        }
        int min=row<col?row:col;
        for(int i=0;i<min-1;i++) {
        	if(matrix[i][i]!=matrix[i+1][i+1]) {
//        		System.out.print("第二个返回"+i+" "+i);
        		return false;
        	}
        }
        for(int k=1;k<row;k++) {
			for (int i = 1; i < row; i++) {
				for (int j = 0; j < col; j++) {
					if (i-j == k&&matrix[k][0]!=matrix[i][j]) {
//						System.out.print("第三个返回"+i+" "+j);
						return false;
					}
				}
			}
        }
        for(int k=1;k<col;k++) {
			for (int j = 1; j < col; j++) {
				for (int i = 0; i < row; i++) {
					if(j-i==k&&matrix[0][k]!=matrix[i][j]) {
//						System.out.print("第四个返回"+j+" "+i+" "+matrix[i][j]);
						return false;
					}
				}
			}
        }
        
    	return true;
    }	
    @Test public void testToeplizMatrix() {
    	int[][] toepliz= {{1,23,3},{12,3,46},{23,1,7}};
    	int[][] a= {{11,74,0,92},{1,11,74,2}};
    	int[][] b= {{1,2,3,4},{5,1,2,3},{9,5,1,2}};
    	System.out.println(isToeplitzMatrix(toepliz));
    	System.out.println(isToeplitzMatrix(a));
    	System.out.println(isToeplitzMatrix(b));
    }
}
